<?php
	require_once "header.php";
	//session_start();


	
	$cFilter = new CFilter;
	$isSuccess = 0 ; 
	if(isset($_POST['btn_ok'])  && isset ($_POST['hCategoryAction']) )
	{
		require_once "trackingModify.php";	
		$action = $_POST['hCategoryAction'];
		
		if( $action === "add")
		{
			$name = $cFilter -> filter ($_POST['tf_name']);
			$description = $_POST['tf_description'];
			$status = 1; 
			
			
			$category = new CCategory;
			
			if($name=="")
			{
				echo '<script language="javascript">window.location.href="main.php?jmp=categories&to=idbadd&name='.$name.'&notification=CATEGORY_ACTION_ADD_ERROR"</script>';
				return ;
			}
			
			$isSuccess = $category-> CCategoryAdd ($name, $description, $status);	
			
			
			if($isSuccess ==-1)
				echo '<script language="javascript">window.location.href="main.php?jmp=categories&to=idbadd&name='.$name.'&notification=XXXX_ACTION_TO_SERVER"</script>';  
	else
			if($isSuccess !=0)
				echo '<script language="javascript">window.location.href="main.php?jmp=catetype&to=idbview&notification=CATEGORY_ACTION_ADD_SUCCESS"</script>';  
		}
		
		if($action ==="edi")
		{
			$isSuccess = 0 ;
			$id= $_POST['showCategory'];
			
			$name = $cFilter -> filter ($_POST['tf_name']);
			
			if($name=="")
			{
				echo '<script language="javascript">window.location.href="main.php?jmp=categories&to=idbedi&idbpos='.$id.'&notification=CATEGORY_ACTION_EDIT_ERROR"</script>';
				return ;
			}
			
			$description = $_POST['tf_description'];
			$status = $_POST['rdbtn_status'];
			
			if($name != "")
			{
				$cCategory =  new CCategory;
				$isSuccess = $cCategory -> CCategoryUpdate($id, $name, $description,$status);
				
				if ($isSuccess <= 0)
				{
					echo '<script language="javascript">window.location.href="main.php?jmp=categories&to=idbedi&idbpos='.$id.'&notification=XXXX_ACTION_TO_SERVER"</script>';
				}
				else
				{
					echo '<script language="javascript">window.location.href="main.php?jmp=catetype&to=idbview&notification=CATEGORY_ACTION_EDIT_SUCCESS"</script>';				
					return ;
				}
			}			
			
		}
		
		
		
		if($action ==="del" )
		{
			$id= $_POST['showCategory'];
			
			$cCategory = new CCategory;
			$cType = new CType;
			
			$arrType = $cType->CTypeListOfCategory($id);
			
			$numOfType = 0 ;
			if(is_array($arrType))
			{
				$numOfType = count ($arrType);
			}
	

			
			if($numOfType <=0)
			{
				$isSuccess = $cCategory -> CCategoryDelete($id);
				
				if ($isSuccess <= 0)
				{
					echo '<script language="javascript">window.location.href="main.php?jmp=categories&to=idbdel&idbpos='.$id.'&notification=XXXX_ACTION_TO_SERVER"</script>';
				}
				else
				{
					echo '<script language="javascript">window.location.href="main.php?jmp=catetype&to=idbview&notification=CATEGORY_ACTION_DEL_SUCCESS"</script>';  
					return ;
				}				
				
			}
			else
			{
					echo '<script language="javascript">window.location.href="main.php?jmp=categories&to=idbdel&idbpos='.$id.'&notification=CATEGORY_ACTION_DEL_ERROR"</script>';				
			}
		}
		
	}
	else
	{
		echo '<script language="javascript">window.location.href="main.php?jmp=catetype&to=idbview"</script>';				
	}
		
	

	//if($isSuccess ==-1)
	//{
	  		//thong bao insert da bi loi
	//}
	
	
	


?>